package com.sihan.swvp.action.video;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.dao.video.DjdDao;

@Controller
@RequestMapping("/djd")
public class DjdAction {
	
	@Autowired
	private DjdDao djdDao;
	
	/**获取任务下的角色
	 * @param taskId 任务Id
	 * @return {roles:[{},{}],gcs:[{},...],videos:{gcid:[],...}}
	 */
	@RequestMapping("/getJs")
	@ResponseBody
	public Object getJs(String taskId,String bzid,int taskType){
		try {
			List<Map<String ,Object>> roles = djdDao.getJs(taskId,taskType);
			List<Map<String ,Object>> gcs = djdDao.getGcs(taskId);
			for (Map<String, Object> role : roles) {
				Map<Object,Object> videos = new HashMap<Object, Object>();
				role.put("vsize", 0);
				String roleid = CommonUtil.o2string(role.get("jsid"));
				for (Map<String, Object> gc : gcs) {
					String gcid = CommonUtil.o2string(gc.get("id"));
					List<Map<String ,Object>> gcvideos = djdDao.getGcVideos(taskId,roleid,gcid);
					videos.put(gc.get("id"), gcvideos);
					long time = 0;
					for (Map<String, Object> map : gcvideos) {
						Long t = CommonUtil.o2long(map.get("videotime"));
						if(null != t) time += t/1000;
					}
					videos.put("gctime", time);
					role.put("vsize", (int)role.get("vsize")+gcvideos.size());
					Long mt = CommonUtil.o2long(gc.get("mt"));
					mt = (mt == null ? 0l : mt);
					gc.put("mt", mt > time ? mt : time);
				}
				
				role.put("videos", videos);
			}
			
			Map<String,Object> rst = new HashMap<String, Object>();
			rst.put("roles", roles);
			rst.put("gcs", gcs);
			
			return rst;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	/**获取任务下的角色
	 * @param taskId 任务Id
	 * @return
	 */
	@RequestMapping("/getGcs")
	@ResponseBody
	public Object getGcs(String taskId,String bzid){
		List<Map<String ,Object>> list = djdDao.getGcs(taskId);
		
		return list;
	}
}
